package com.sbpm.util;

import org.drools.core.marshalling.impl.ClassObjectMarshallingStrategyAcceptor;
import org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy;
import org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy;
import org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory;
import org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder;
import org.jbpm.services.task.identity.JBossUserGroupCallbackImpl;
import org.jbpm.services.task.lifecycle.listeners.BAMTaskEventListener;
import org.kie.api.io.ResourceType;
import org.kie.api.marshalling.ObjectMarshallingStrategy;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.task.TaskService;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.manager.RuntimeEnvironment;
import org.kie.api.runtime.manager.RuntimeManagerFactory;
import org.kie.internal.runtime.manager.context.EmptyContext;
import org.kie.internal.task.api.UserGroupCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

@Component
public class JbpmConfiguration
{

    /*private RuntimeManager runtimeManager;
    private RuntimeEngine runtime;
    private KieSession ksession;*/

    @Bean
    @Qualifier( "runtimeManager" )
    public RuntimeManager getRuntimeManager( @Qualifier( "jbpmEntityManagerFactory" ) EntityManagerFactory jbpmEntityManagerFactory, UserGroupCallback userGroupCallback ) {
        System.out.println( "!!!!!!!!!!! getRuntimeManager()" );
        //UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl("classpath:/usergroup.properties");
//        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "org.jbpm.persistence.jpa" );
        DefaultRegisterableItemsFactory registerableItemsFactory = new DefaultRegisterableItemsFactory();
        registerableItemsFactory.addTaskListener( BAMTaskEventListener.class );
        RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault()
                .entityManagerFactory( jbpmEntityManagerFactory )
                .userGroupCallback( userGroupCallback )
                .registerableItemsFactory( registerableItemsFactory )
                .addEnvironmentEntry( EnvironmentName.OBJECT_MARSHALLING_STRATEGIES,
                        new ObjectMarshallingStrategy[]{
                                new JPAPlaceholderResolverStrategy( jbpmEntityManagerFactory ),
                                new SerializablePlaceholderResolverStrategy(
                                        ClassObjectMarshallingStrategyAcceptor.DEFAULT )
                        } )
                        //.addAsset(ResourceFactory.newClassPathResource("taskexample/SampleHumanTaskFormVariables.bpmn"), ResourceType.BPMN2)
                .addAsset( ResourceFactory.newClassPathResource( "processes/InsuranceRequest.bpmn" ), ResourceType.BPMN2 )
                .addAsset( ResourceFactory.newClassPathResource( "processes/Transfer.bpmn" ), ResourceType.BPMN2 )
                .get();

        RuntimeManager runtimeManager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager( environment, "springmanager" );

        return runtimeManager;
    }

    @Bean
    public RuntimeEngine getRuntimeEngine( RuntimeManager runtimeManager )
    {
        System.out.println( "!!!!!!!!!!! getRuntimeEngine()" );
        return runtimeManager.getRuntimeEngine(EmptyContext.get());
    }

    @Bean
    public KieSession getSingletonSession( RuntimeEngine runtimeEngine ){
        System.out.println( "!!!!!!!!!!! getSingletonSession()" );
        return runtimeEngine.getKieSession();
    }


    @Bean
    public TaskService getTaskService( RuntimeEngine runtimeEngine ){
        System.out.println( "!!!!!!!!!!! getTaskService()" );
        return runtimeEngine.getTaskService();
    }


}
